package com.ncr.travel.appmgr.service.provider.impl;

import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ncr.travel.appmgr.model.shared.Product;
import com.ncr.travel.appmgr.model.shared.ProductCategory;
import com.ncr.travel.appmgr.service.config.ConfigurationCache;
import com.ncr.travel.appmgr.service.converter.ProductConverter;
import com.ncr.travel.appmgr.service.provider.ProductsProvider;
import com.ncr.travel.tas.configuration.model.ConfigurationValue;
import com.ncr.travel.tas.shared.configuration.ConfigurationKey;
import com.ncr.travel.tas.shared.configuration.ConfigurationNodeEnum;
import com.ncr.travel.tas.shared.configuration.ConfigurationProductKey;

@Component
@Transactional
public class ProductsProviderImpl implements ProductsProvider {
	private static final Logger logger = LoggerFactory.getLogger(ProductsProviderImpl.class);

	private final ConfigurationCache configurationCache;

	private final ProductConverter productConverter;

	@Autowired
	public ProductsProviderImpl(ProductConverter productConverter, ConfigurationCache configurationCache) {
		this.productConverter = productConverter;
		this.configurationCache = configurationCache;
	}

	/**
	 * @throws Exception
	 * @see com.ncr.travel.appmgr.service.provider.com.ncr.travel.appmgr.service.provider.ProductsProvider#getProducts(java.lang.String)
	 */
	@Override
	public List<Product> getProducts(String userName) throws Exception {

		Set<ConfigurationValue> values = configurationCache.getConfigurationValues(ConfigurationNodeEnum.APPMGR, ConfigurationProductKey.APPLICATION_MANAGER_UI, ConfigurationKey.PRODUCTS);

		List<Product> products = productConverter.getProducts(values);

		return products;
	}

	/**
	 * @see com.ncr.travel.appmgr.service.provider.com.ncr.travel.appmgr.service.provider.ProductsProvider#getProductCategories(java.lang.String)
	 */
	@Override
	public List<ProductCategory> getProductCategories(String productName) {

		List<ProductCategory> productCategories = productConverter.getProductCategories(productName);

		return productCategories;
	}
}
